﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tesla.Service.WcfServiceCommon;
using System.ServiceModel;
using Tesla.Service.ServiceContracts;
using Tesla.Service.DataContracts;
using Tesla.Util.ValidateEntity;


namespace Tesla.Web
{
    public class TaskServiceDispatcher
    {
        public IList<BaseDC> DisplayAllTask(int gridSize, ref int? totalRecordCount)
        {

            ISvcTask _proxySvcTask;
            ChannelFactory<ISvcTask> chfTask = null; IList<BaseDC> Tasks = null;

            try
            {
                chfTask = GADNChannelFactoryProvider.CreateChannelFactory<ISvcTask>();
                _proxySvcTask = chfTask.CreateChannel();
                Tasks = _proxySvcTask.DisplayAll(gridSize, ref totalRecordCount);
            }
            catch (FaultException gfx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfTask.Close();
            }
            return Tasks;
        }



        public IList<BaseDC> SearchTaskByName(string name, int gridSize, ref int? totalRecordCount)
        {
            ISvcTask _proxySvcTask;
            ChannelFactory<ISvcTask> chfTask = null; IList<BaseDC> Tasks = null;

            try
            {
                chfTask = GADNChannelFactoryProvider.CreateChannelFactory<ISvcTask>();
                _proxySvcTask = chfTask.CreateChannel();
                Tasks = _proxySvcTask.SearchByName(name, gridSize, ref totalRecordCount);
            }
            catch (FaultException gfx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfTask.Close();
            }
            return Tasks;
        }

        public IList<BaseDC> ReadTask(int id)
        {
            ISvcTask _proxySvcTask;
            ChannelFactory<ISvcTask> chfTask = null; IList<BaseDC> Tasks = null;

            try
            {
                chfTask = GADNChannelFactoryProvider.CreateChannelFactory<ISvcTask>();
                _proxySvcTask = chfTask.CreateChannel();
                Tasks = _proxySvcTask.Read(id);
            }
            catch (FaultException gfx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfTask.Close();
            }
            return Tasks;
        }

        public IList<BaseDC> ReadTaskByRowNumberRange(int startRecordNo, int endRecordNo, string sortColName, string sortDirection, string searchString)
        {
            ISvcTask _proxySvcTask;
            ChannelFactory<ISvcTask> chfTask = null; IList<BaseDC> Tasks = null;

            try
            {
                chfTask = GADNChannelFactoryProvider.CreateChannelFactory<ISvcTask>();
                _proxySvcTask = chfTask.CreateChannel();
                Tasks = _proxySvcTask.ReadByRange(startRecordNo, endRecordNo, sortColName, sortDirection, searchString);
            }
            catch (FaultException gfx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfTask.Close();
            }
            return Tasks;
        }


        public int? CreateTask(BaseDC bdc)
        {
            ISvcTask _proxySvcTask;
            ChannelFactory<ISvcTask> chfTask = null; int? success = 0;

            try
            {
                chfTask = GADNChannelFactoryProvider.CreateChannelFactory<ISvcTask>();
                _proxySvcTask = chfTask.CreateChannel();
                success = _proxySvcTask.Create(bdc);
            }
            catch (FaultException gfx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
                throw gfx;
            }
            catch (TimeoutException tx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfTask.Close();
            }
            return success;
        }

        public int UpdateTask(BaseDC bdc)
        {
            ISvcTask _proxySvcTask;
            ChannelFactory<ISvcTask> chfTask = null; int updateResult = 0;

            try
            {
                chfTask = GADNChannelFactoryProvider.CreateChannelFactory<ISvcTask>();
                _proxySvcTask = chfTask.CreateChannel();
                updateResult = _proxySvcTask.Update(bdc);
            }
            catch (FaultException gfx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
                throw gfx;
            }
            catch (TimeoutException tx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfTask.Close();
            }
            return updateResult;
        }

        public IList<BaseDC> ReadAllTasks(int taskID)
        {
            ISvcTask _proxySvcTask;
            ChannelFactory<ISvcTask> chfTask = null; IList<BaseDC> tasks = null;

            try
            {
                chfTask = GADNChannelFactoryProvider.CreateChannelFactory<ISvcTask>();
                _proxySvcTask = chfTask.CreateChannel();
                tasks = _proxySvcTask.ReadAllTasks(taskID);
            }
            catch (FaultException gfx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfTask.Close();
            }
            return tasks;
        }

        public IList<BaseDC> ReadAllTaskTypes()
        {
            ISvcTask _proxySvcTask;
            ChannelFactory<ISvcTask> chfTask = null; IList<BaseDC> taskTypes = null;

            try
            {
                chfTask = GADNChannelFactoryProvider.CreateChannelFactory<ISvcTask>();
                _proxySvcTask = chfTask.CreateChannel();
                taskTypes = _proxySvcTask.ReadAllTaskTypes();
            }
            catch (FaultException gfx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfTask.Close();
            }
            return taskTypes;
        }

        public IList<BaseDC> ReadAllPhases()
        {
            ISvcTask _proxySvcTask;
            ChannelFactory<ISvcTask> chfTask = null; IList<BaseDC> phases = null;

            try
            {
                chfTask = GADNChannelFactoryProvider.CreateChannelFactory<ISvcTask>();
                _proxySvcTask = chfTask.CreateChannel();
                phases = _proxySvcTask.ReadAllPhases();
            }
            catch (FaultException gfx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfTask.State == CommunicationState.Faulted || chfTask.State == CommunicationState.Opened)
                    chfTask.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfTask.Close();
            }
            return phases;
        }
    }
}

